app.controller('baseController', function ($scope, $location, baseService) {
    $scope.cache = {};
    $scope.entity = {};

    $scope.ids = [];
    $scope.updIds = function ($event, id) {
        var checkbox = $event.target;
        if (checkbox.checked) {
            $scope.ids.push(parseInt(id));
        } else {
            var idIndex = $scope.ids.indexOf(id);
            $scope.ids.splice(idIndex, 1);
        }
    };
    $scope.allIds = function($event, records) {
        $scope.ids = [];
        var checkbox = $event.target;
        if (checkbox.checked) {
            for(let i in records){
                let record = records[i];
                $scope.ids.push(parseInt(record.id));
            }
            $('.js_checkbox').prop("checked", true);
        }else{
            $('.js_checkbox').prop("checked", false);
        }
    };

    $scope.pageCtl = {
        current : 1,
        size : 15,
        hasFirst : true,
        hasFinally : true,
        hasPrevious : true,
        hasNext : true,
        pageListMax : 5, //展示页数上线
        pageList : []
    };

    $scope.findPage = function (page, size) {
        var queryJson = {};
        if(!page || !size){
            queryJson.page = {
                current : $scope.pageCtl.current,
                size : $scope.pageCtl.size,
            };
        }
        queryJson.keywords = $scope.keywords != null ? $scope.keywords : null;
        queryJson.entity = $scope.entity !== {} ? $scope.entity : null;

        baseService.findPage(queryJson).success(function (response) {
            $scope.resHandler(response, function (response) {
                $scope.page = response.data;
                $scope.genPageControl();
                $scope.page = $scope.pageHandler($scope.page);
            })
        });

    };

    $scope.genPageControl =  function () {
        var page = $scope.page;
        var pageCtl = $scope.pageCtl;

        //页数列表
        var start, end;
        if (pageCtl.current <= pageCtl.pageListMax) {
            start = 1;
            end = pageCtl.pageListMax;
        } else if (pageCtl.current >= (page.pages - pageCtl.pageListMax)) {
            //后N页（当前页控件没居中）
            start = (page.pages - pageCtl.pageListMax);
            end = page.pages;
        } else {
            var temp;
            if (pageCtl.pageListMax % 2 === 0) {
                temp = pageCtl.pageListMax / 2;
                start = pageCtl.current - temp;
                end = pageCtl.current + temp;
            } else {
                temp = Math.floor(pageCtl.pageListMax / 2);
                start = pageCtl.current - temp;
                end = pageCtl.current + temp;
            }
        }
        //中N页（当前页控件居中）
        start = start <= 0 ? 1 : start;
        end = end > page.pages ? page.pages : end;
        pageCtl.pageList = $scope.range(end, start);

        if (pageCtl.current === 1) pageCtl.hasPrevious = false;
        if (pageCtl.current === page.pages) pageCtl.hasNext = false;
        if (pageCtl.current <= 1) pageCtl.hasFirst = false;
        if (pageCtl.current > page.pages - 2) pageCtl.hasFinally = false;

        $scope.pageCtl = pageCtl;
    }

    $scope.updateManyById = function(direct){
        baseService.save({"entity": $scope.entity}).success(function (response) {
            $scope.resHandler(response, function (response) {
                if (direct != null || direct !== "") {
                    location.href = direct;
                } else {
                    alert(response.msg);
                }
            })
        });
    }

    $scope.find = function () {
        baseService.find({"entity": $scope.entity}).success(function (response) {
            $scope.resHandler(response, function (response) {
                $scope.entity = response.data;
            });
        });
    };

    $scope.range = function(end, start) {
        var arr = [];
        if (start == null) start = 0;
        for (var i = start; i <= end; i++) arr.push(i);
        return arr;
    }

    $scope.resHandler = function (response, succCallback) {
        if (response.success) {
            if (succCallback === undefined || succCallback === null) {
                alert($scope.successMsg);
                location.reload();
            } else {
                succCallback(response);
            }
        } else {
            alert(response.msg);
        }
    }

    $scope.pageHandler = function (page) {
        return page;
    }

    // $scope.editInit = function () {}

    // $scope.listInit = function () {}
});